/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterFrame.java
 *
 * Created on 2009-10-27, 20:59:01
 */
package View;
import Model.CustomerI;
import Control.CustomerServiceI;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class RegisterFrame extends javax.swing.JFrame {

    CustomerServiceI csi;
    ArrayList list = null;

    /** Creates new form RegisterFrame */
    public RegisterFrame() {
        super("Register");
        initComponents();
        csi = new CustomerServiceI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        submit_jbt = new javax.swing.JButton();
        user_jlb = new javax.swing.JLabel();
        password_jlb = new javax.swing.JLabel();
        repeatpassword_jlb = new javax.swing.JLabel();
        email_jlb = new javax.swing.JLabel();
        tel_jlb = new javax.swing.JLabel();
        id_jlb = new javax.swing.JLabel();
        location_jlb = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        return_jbt = new javax.swing.JButton();
        user_jtf = new javax.swing.JTextField();
        email_jtf = new javax.swing.JTextField();
        tel_jtf = new javax.swing.JTextField();
        location_jtf = new javax.swing.JTextField();
        check_jbt = new javax.swing.JButton();
        id_jtf = new javax.swing.JTextField();
        password_jpf = new javax.swing.JPasswordField();
        repeatpassword_jpf = new javax.swing.JPasswordField();
        people_jlb = new javax.swing.JLabel();
        people_jcb = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        submit_jbt.setBackground(new java.awt.Color(51, 51, 51));
        submit_jbt.setForeground(new java.awt.Color(255, 255, 25));
        submit_jbt.setText("Submit");
        submit_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submit_jbtActionPerformed(evt);
            }
        });
        submit_jbt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                submitKeyPressedActionPerformed(evt);
            }
        });

        user_jlb.setBackground(new java.awt.Color(255, 255, 255));
        user_jlb.setForeground(new java.awt.Color(51, 51, 51));
        user_jlb.setText("User:");

        password_jlb.setBackground(new java.awt.Color(255, 255, 255));
        password_jlb.setForeground(new java.awt.Color(51, 51, 51));
        password_jlb.setText("Password:");

        repeatpassword_jlb.setForeground(new java.awt.Color(51, 51, 51));
        repeatpassword_jlb.setText("Repeat Password :");

        email_jlb.setForeground(new java.awt.Color(51, 51, 51));
        email_jlb.setText("Email:");

        tel_jlb.setForeground(new java.awt.Color(51, 51, 51));
        tel_jlb.setText("Tel:");

        id_jlb.setForeground(new java.awt.Color(51, 51, 51));
        id_jlb.setText("ID Num:");

        location_jlb.setForeground(new java.awt.Color(51, 51, 51));
        location_jlb.setText("Location:");

        jTextField1.setText("*************");

        return_jbt.setBackground(new java.awt.Color(51, 51, 51));
        return_jbt.setForeground(new java.awt.Color(255, 255, 25));
        return_jbt.setText("Return Login");
        return_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                return_jbtActionPerformed(evt);
            }
        });

        user_jtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                user_jtfActionPerformed(evt);
            }
        });

        email_jtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                email_jtfActionPerformed(evt);
            }
        });

        tel_jtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tel_jtfActionPerformed(evt);
            }
        });

        location_jtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                location_jtfActionPerformed(evt);
            }
        });

        check_jbt.setBackground(new java.awt.Color(51, 51, 51));
        check_jbt.setForeground(new java.awt.Color(255, 255, 25));
        check_jbt.setText("Check User Name");
        check_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                check_jbtActionPerformed(evt);
            }
        });

        id_jtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                id_jtfActionPerformed(evt);
            }
        });

        password_jpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                password_jpfActionPerformed(evt);
            }
        });

        repeatpassword_jpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repeatpassword_jpfActionPerformed(evt);
            }
        });

        people_jlb.setText("People:");

        people_jcb.setBackground(new java.awt.Color(102, 102, 0));
        people_jcb.setEditable(true);
        people_jcb.setForeground(new java.awt.Color(153, 153, 153));
        people_jcb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Manager", "Customer" }));
        people_jcb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                people_jcbActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(repeatpassword_jlb)
                    .addComponent(password_jlb)
                    .addComponent(user_jlb)
                    .addComponent(email_jlb)
                    .addComponent(tel_jlb)
                    .addComponent(id_jlb)
                    .addComponent(location_jlb)
                    .addComponent(people_jlb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(people_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                        .addComponent(submit_jbt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(return_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(location_jtf, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(id_jtf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(tel_jtf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(email_jtf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(user_jtf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(repeatpassword_jpf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(password_jpf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(check_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(40, 40, 40)
                .addComponent(jTextField1, 0, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(user_jlb, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(user_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(check_jbt))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password_jlb, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(password_jpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(repeatpassword_jlb)
                            .addComponent(repeatpassword_jpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(email_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(email_jlb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tel_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tel_jlb))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(id_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(id_jlb, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(location_jlb)
                            .addComponent(location_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(return_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(submit_jbt)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(people_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(people_jlb))))
                .addGap(70, 70, 70))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void submit_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submit_jbtActionPerformed
        //User is empty
        String user = user_jtf.getText();
        if (user.equals("")) {
            JOptionPane.showMessageDialog(null, "Name don't empty,Please input name!");
            return;
        }
        //Check ID that is empty
        if (id_jtf.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "ID is empty,Please input ID!");
            return;
        }
        //Check id that is repeated
        int id = Integer.valueOf(id_jtf.getText());
        if (checkID(id)) {
            JOptionPane.showMessageDialog(null, "ID has existed yet!");
            return;
        }
        list = (ArrayList) csi.findAll();
        String password = password_jpf.getText();
        if (list != null) {
            if (!repeatpassword_jpf.getText().equals(password)) {
                JOptionPane.showMessageDialog(null, "Password don't equals repeatpassword,Please input same password!");
                return;
            } else if (checkUserName(user)) {
                JOptionPane.showMessageDialog(null, "Name has existed yet!");
                return;
            }
        }
        CustomerI ci = new CustomerI();
        ci.setId(id);
        ci.setUser(user);
        ci.setPassword(password);
        ci.setEmail(email_jtf.getText());
        ci.setTel(tel_jtf.getText());
        ci.setLocation(location_jtf.getText());
        ci.setPeople(people_jcb.getSelectedItem().toString());//jcb的输入

        csi.insertCustomer(ci);
        JOptionPane.showMessageDialog(null, "Register Success");
        clearAllComponent();
}//GEN-LAST:event_submit_jbtActionPerformed

    private void return_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_return_jbtActionPerformed
        this.dispose();//close the frame in order to keep one frame always
        LoginFrame login = new LoginFrame();
        login.setVisible(true);
}//GEN-LAST:event_return_jbtActionPerformed

    private void check_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_check_jbtActionPerformed
        String user = user_jtf.getText();
        if (user.equals("")) {
            JOptionPane.showMessageDialog(null, "Name don't empty,Please input name!");
            return;
        }
        if (checkUserName(user)) {
            JOptionPane.showMessageDialog(null, "Name has existed yet!");
            return;
        } else {
            JOptionPane.showMessageDialog(null, "Register!");
        }
    }//GEN-LAST:event_check_jbtActionPerformed

    private void location_jtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_location_jtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_location_jtfActionPerformed

    private void tel_jtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tel_jtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tel_jtfActionPerformed

    private void email_jtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_email_jtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_email_jtfActionPerformed

    private void submitKeyPressedActionPerformed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_submitKeyPressedActionPerformed
        // TODO add your handling code here:
          //User is empty
        String user = user_jtf.getText();
        if (user.equals("")) {
            JOptionPane.showMessageDialog(null, "Name don't empty,Please input name!");
            return;
        }
        //Check ID that is empty
        if (id_jtf.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "ID is empty,Please input ID!");
            return;
        }
        //Check id that is repeated
        int id = Integer.valueOf(id_jtf.getText());
        if (checkID(id)) {
            JOptionPane.showMessageDialog(null, "ID has existed yet!");
            return;
        }
        list = (ArrayList) csi.findAll();
        String password = password_jpf.getText();
        if (list != null) {
            if (!repeatpassword_jpf.getText().equals(password)) {
                JOptionPane.showMessageDialog(null, "Password don't equals repeatpassword,Please input same password!");
                return;
            } else if (checkUserName(user)) {
                JOptionPane.showMessageDialog(null, "Name has existed yet!");
                return;
            }
        }
        CustomerI ci = new CustomerI();
        ci.setId(id);
        ci.setUser(user);
        ci.setPassword(password);
        ci.setEmail(email_jtf.getText());
        ci.setTel(tel_jtf.getText());
        ci.setLocation(location_jtf.getText());

        csi.insertCustomer(ci);
        JOptionPane.showMessageDialog(null, "Register Success");
        clearAllComponent();
    }//GEN-LAST:event_submitKeyPressedActionPerformed

    private void people_jcbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_people_jcbActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_people_jcbActionPerformed

    private void user_jtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_user_jtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_user_jtfActionPerformed

    private void password_jpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_password_jpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_password_jpfActionPerformed

    private void repeatpassword_jpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repeatpassword_jpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_repeatpassword_jpfActionPerformed

    private void id_jtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_id_jtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_id_jtfActionPerformed

    public boolean checkUserName(String username) {
        list = (ArrayList) csi.findAll();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (((CustomerI) list.get(i)).getUser().equals(username)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean checkID(int id) {
        list = (ArrayList) csi.findAll();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (((CustomerI) list.get(i)).getId() == id) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegisterFrame().setVisible(true);
            }
        });
    }

    public void clearAllComponent() {
        id_jtf.setText("");
        user_jtf.setText("");
        password_jpf.setText("");
        repeatpassword_jpf.setText("");
        email_jtf.setText("");
        location_jtf.setText("");
        tel_jtf.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton check_jbt;
    private javax.swing.JLabel email_jlb;
    private javax.swing.JTextField email_jtf;
    private javax.swing.JLabel id_jlb;
    private javax.swing.JTextField id_jtf;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel location_jlb;
    private javax.swing.JTextField location_jtf;
    private javax.swing.JLabel password_jlb;
    private javax.swing.JPasswordField password_jpf;
    private javax.swing.JComboBox people_jcb;
    private javax.swing.JLabel people_jlb;
    private javax.swing.JLabel repeatpassword_jlb;
    private javax.swing.JPasswordField repeatpassword_jpf;
    private javax.swing.JButton return_jbt;
    private javax.swing.JButton submit_jbt;
    private javax.swing.JLabel tel_jlb;
    private javax.swing.JTextField tel_jtf;
    private javax.swing.JLabel user_jlb;
    private javax.swing.JTextField user_jtf;
    // End of variables declaration//GEN-END:variables
}
